package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "房间信息管理")
@RestController
@RequestMapping("/admin/room")
public class RoomController {

    @Autowired
    private RoomInfoService roomInfoService ;

    @Autowired
    private GraphInfoService graphInfoService ;

    @Autowired
    private RoomAttrValueService roomAttrValueService ;

    @Autowired
    private RoomFacilityService roomFacilityService ;

    @Autowired
    private RoomLabelService roomLabelService ;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService ;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService ;

    @Operation(summary = "保存或更新房间信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody RoomSubmitVo roomSubmitVo) {
        Long id = roomSubmitVo.getId();
        if(id!=null){//表示修改，因此需要先删除各种旧关系
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType,ItemType.ROOM)) ;
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));
        }
        // 1. 新增或修改房间基本信息
        roomInfoService.saveOrUpdate(roomSubmitVo);

        // 2. 新增各种关联关系信息
        graphInfoService.saveBatch(roomSubmitVo.getGraphVoList().stream().map(graphVo -> {
            GraphInfo graphInfo = new GraphInfo() ;
            BeanUtils.copyProperties(graphVo,graphInfo);
            graphInfo.setItemType(ItemType.ROOM);
            graphInfo.setItemId(id);
            return graphInfo ;
        }).collect(Collectors.toList()));

        roomAttrValueService
                .saveBatch(roomSubmitVo.getAttrValueIds()
                        .stream()
                        .map(attrValueId -> RoomAttrValue.builder().roomId(id).attrValueId(attrValueId).build())
                        .collect(Collectors.toList()));
        roomFacilityService
                .saveBatch(roomSubmitVo.getFacilityInfoIds()
                        .stream()
                        .map(facilityId -> RoomFacility.builder().roomId(id).facilityId(facilityId).build())
                        .collect(Collectors.toList()));

        roomLabelService
                .saveBatch(roomSubmitVo.getLabelInfoIds()
                        .stream()
                        .map(labelId -> RoomLabel.builder().roomId(id).labelId(labelId).build())
                        .collect(Collectors.toList()));

        roomPaymentTypeService
                .saveBatch(roomSubmitVo.getPaymentTypeIds()
                        .stream()
                        .map(paymentTypeId -> RoomPaymentType.builder().roomId(id).paymentTypeId(paymentTypeId).build())
                        .collect(Collectors.toList()));

        roomLeaseTermService
                .saveBatch(roomSubmitVo.getLeaseTermIds()
                        .stream()
                        .map(leaseTermId -> RoomLeaseTerm.builder().roomId(id).leaseTermId(leaseTermId).build())
                        .collect(Collectors.toList()));


        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询房间列表")
    @GetMapping("pageItem")
    public Result<IPage<RoomItemVo>> pageItem(@RequestParam long current, @RequestParam long size, RoomQueryVo queryVo) {
        IPage<RoomItemVo> roomItemVoIPage = roomInfoService.pageItem(Page.of(current,size) , queryVo);
        return Result.ok(roomItemVoIPage);
    }

    @Operation(summary = "根据id获取房间详细信息")
    @GetMapping("getDetailById")
    public Result<RoomDetailVo> getDetailById(@RequestParam Long id) {
        RoomDetailVo roomDetailVo = roomInfoService.getDetailById3(id);
        return Result.ok(roomDetailVo);
    }

    @Operation(summary = "根据id删除房间信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        roomInfoService.removeRoomById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id修改房间发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(Long id, ReleaseStatus status) {
        return Result.ok();
    }

    @GetMapping("listBasicByApartmentId")
    @Operation(summary = "根据公寓id查询房间列表")
    public Result<List<RoomInfo>> listBasicByApartmentId(Long id) {
        return Result.ok();
    }

}


















